package cn.aezo.minions.web.controller;

import cn.aezo.minions.gen.mapper.ProjectConfigDao;
import cn.aezo.minions.gen.mapper.ProjectModuleDao;
import cn.aezo.minions.gen.mapper.TemplateDao;
import cn.aezo.minions.gen.model.ProjectConfig;
import cn.aezo.minions.gen.model.ProjectModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by smalle on 2017/10/23.
 */
@Controller // 此时不能是@RestController
@RequestMapping("/manage")
public class PageController {
    @Autowired
    HttpServletRequest request;

    @Autowired
    private ProjectConfigDao projectConfigDao;

    @Autowired
    private ProjectModuleDao projectModuleDao;

    @Autowired
    TemplateDao templateDao;

    @GetMapping("/projectConfig")
    public String projectConfig(Model model, Long projectConfigId) {
        ProjectConfig projectConfig = projectConfigDao.findOne(projectConfigId);

        model.addAttribute("projectConfig", projectConfig);

        return "/pages/projectConfig";
    }

    @GetMapping("/userTemplate")
    public String userTemplate(Model model) {
        String userId = CommonController.getUserId(request);
        List<Map<String, Object>> templateList = templateDao.findTemplateByUserId(Long.valueOf(userId));
        model.addAttribute("templateList", templateList);

        return "/pages/userTemplate";
    }

    @GetMapping("/projectConfig/editModule")
    public String editModule(Model model, Long projectModuleId) {
        ProjectModule projectModule = new ProjectModule();
        if(projectModuleId != null) {
            projectModule = projectModuleDao.findOne(projectModuleId);
        }
        model.addAttribute("projectModule", projectModule);

        return "/pages/projectConfig_editModule";
    }

    @GetMapping("/userTemplate/edit")
    public String editTemplate(Model model, Long templateId) {
        return "/pages/userTemplate_edit";
    }

    @GetMapping("/login")
    public String login() {
        return "/login";
    }

    @GetMapping("/")
    public String index() {
        return "/index";
    }

    @GetMapping("/index")
    public String index1() {
        return "/index";
    }

    @GetMapping("/workbench")
    public String workbench() {
        return "/workbench";
    }

    @GetMapping("/about")
    public String about() {
        return "/about";
    }

    @GetMapping("/403")
    public String error403() {
        return "/error/403";
    }

    @GetMapping("/404")
    public String error404() {
        return "/error/404";
    }
}
